<template>
	<view class="form-box">
		<Registration ref="Registration" :list="listFrom" :listData="model" :listRules="rules" @submit="submit" />
	</view>
</template>
<script>
	import Registration from "./form/index.vue";
	import {
		register,
		registerInfo,
		allStore
	} from "@/api/axios/center.js";
	export default {
		data() {
			return {
				listFrom: [{
						type: "select",
						name: "storeValue",
						label: "选择分院",
						show: false,
						arr: [],
					},
					{
						type: "input",
						name: "weUserName",
						label: "姓名",
					},
					{
						type: "input",
						name: "phoneNumber",
						label: "联系方式	",
					},
					{
						type: "input",
						name: "email",
						label: "邮件",
					},

					{
						type: "select",
						name: "sex",
						label: "性别",
						show: false,
						arr: [
							[{
									name: "男",
									id: 1,
								},
								{
									name: "女",
									id: 0,
								},
							],
						],
					},
					{
						type: "date",
						name: "birthDate",
						label: "出生日期",
						show: false,
					},
					{
						type: "radio",
						name: "birthDateType",
						label: "出生日期类型",
						arr: [{
								name: "农历",
								value: "1",
								disabled: false,
							},
							{
								name: "阳历",
								disabled: false,
								value: "2",
							},
						],
					},
					{
						type: "number",
						name: "age",
						label: "年龄",
					},
					{
						type: "input",
						name: "profession",
						label: "职业",
					},
					{
						type: "input",
						name: "province",
						label: "省",
					},
					{
						type: "input",
						name: "city",
						label: "市",
					},
					{
						type: "input",
						name: "area",
						label: "区",
					},
					{
						type: "radio",
						name: "approach",
						label: "了解途径",
						arr: [{
								name: "电梯广告",
								value: '1',
								disabled: false,
							},
							{
								name: "朋友推荐",
								value: '2',
								disabled: false,
							},
							{
								name: "户外广告牌",
								value: '3',
								disabled: false,

							},
							{
								name: "直播平台",
								value: '4',
								disabled: false,
							},
							{
								name: "地铁",
								value: '5',
								disabled: false,

							},
							{
								name: "电台广告",
								value: '6',
								disabled: false,

							},
							{
								name: "抖音",
								value: '7',
								disabled: false,

							},
							{
								name: "百度",
								value: '8',
								disabled: false,

							},
							{
								name: "头条",
								value: '9',
								disabled: false,

							},
							{
								name: "美团",
								value: '10',
								disabled: false,

							},
							{
								name: "新氧",
								value: '11',
								disabled: false,

							},
							{
								name: "天猫",
								value: '12',
								disabled: false,

							},
							{
								name: "京东商城",
								value: '13',
								disabled: false,

							},
							{
								name: "其他",
								value: '99',
								disabled: false,
							},
						],

					},
					{
						type: "radio",
						name: "lossHairTime",
						label: "脱发时间",
						arr: [{
								name: "几个月前",
								value: "1",
								disabled: false,
							},
							{
								name: "三年内",
								disabled: false,
								value: "2",
							},
							{
								name: "5年以上",
								disabled: false,
								value: "3",
							},
							{
								name: "无脱发现象",
								disabled: false,
								value: "9",
							},
						],
					},
					{
						type: "number",
						name: "lossHairTimeValue",
						label: "脱发时间（几个月前）",
					},
					{
						type: "radio",
						name: "lossHairOfHistory",
						label: "家中是否有脱发的人",
						arr: [{
								name: "父系",
								value: "1",
								disabled: false,
							},
							{
								name: "母系",
								disabled: false,
								value: "2",
							},
						],
					},
					{
						type: "radio",
						name: "isDietRegular",
						label: "是否饮食规律",
						arr: [{
								name: "是",
								value: "0",
								disabled: false,
							},
							{
								name: "否",
								disabled: false,
								value: "1",
							},
						],
					},
					{
						type: "radio",
						name: "sleepTime",
						label: "入睡时间",
						arr: [{
								name: "22:00前",
								value: "1",
								disabled: false,
							},
							{
								name: "22:00-23:00",
								disabled: false,
								value: "2",
							},
							{
								name: "23:00后",
								disabled: false,
								value: "3",
							},
						],
					},
					{
						type: "radio",
						name: "isSmokingAndDrinking",
						label: "是否吸烟喝酒",
						arr: [{
								name: "是",
								value: "0",
								disabled: false,
							},
							{
								name: "否",
								disabled: false,
								value: "1",
							},
						],
					},
					{
						type: "radio",
						name: "hairCommonProblem",
						label: "头发经常出现的问题",
						arr: [{
								name: "出油",
								value: "1",
								disabled: false,
							},
							{
								name: "越变越细",
								disabled: false,
								value: "2",
							},
							{
								name: "持续脱发",
								disabled: false,
								value: "3",
							},
							{
								name: "正在掉发",
								disabled: false,
								value: "4",
							},
							{
								name: "头屑",
								disabled: false,
								value: "5",
							},
							{
								name: "斑秃",
								disabled: false,
								value: "6",
							},
							{
								name: "毛囊炎",
								disabled: false,
								value: "7",
							},
							{
								name: "白发",
								disabled: false,
								value: "8",
							},
							{
								name: "其他",
								disabled: false,
								value: "9",
							},
						],
					},
					{
						type: "radio",
						name: "isUseMedication",
						label: "是否使用过药物诊疗",
						arr: [{
								name: "是",
								value: "0",
								disabled: false,
							},
							{
								name: "否",
								disabled: false,
								value: "1",
							},
						],
					},
					{
						type: "radio",
						name: "consultProblem",
						label: "咨询问题",
						arr: [{
								name: "植发",
								value: "1",
								disabled: false,
							},
							{
								name: "植眉",
								disabled: false,
								value: "2",
							},
							{
								name: "睫毛",
								disabled: false,
								value: "3",
							},
							{
								name: "胡须",
								disabled: false,
								value: "4",
							},
							{
								name: "鬓角",
								disabled: false,
								value: "5",
							},
							{
								name: "头皮养护",
								disabled: false,
								value: "6",
							},
							{
								name: "发干养护",
								disabled: false,
								value: "7",
							},
							{
								name: "其他",
								disabled: false,
								value: "9",
							},
						],
					},
					{
						type: "radio",
						name: "hairTransplantUnderstandingDegree",
						label: "对植发手术的了解程度",
						arr: [{
								name: "刚了解",
								value: "1",
								disabled: false,
							},
							{
								name: "三个月",
								disabled: false,
								value: "2",
							},
							{
								name: "半年",
								disabled: false,
								value: "3",
							},
							{
								name: "一年以上",
								disabled: false,
								value: "4",
							},
							{
								name: "二年以上",
								disabled: false,
								value: "5",
							},
							{
								name: "三年以上",
								disabled: false,
								value: "6",
							},
						],
					},
					{
						type: "radio",
						name: "hairCareUnderstandingDegree",
						label: "您对养发了解的程度",
						arr: [{
								name: "刚了解",
								value: "1",
								disabled: false,
							},
							{
								name: "三个月",
								disabled: false,
								value: "2",
							},
							{
								name: "半年",
								disabled: false,
								value: "3",
							},
							{
								name: "一年以上",
								disabled: false,
								value: "4",
							},
							{
								name: "二年以上",
								disabled: false,
								value: "5",
							},
							{
								name: "三年以上",
								disabled: false,
								value: "6",
							},
						],
					},
					{
						type: "input",
						name: "consultOrTreatThePlace",
						label: "您去过哪些地方咨询或治疗过",
					},
					{
						type: "radio",
						name: "operationMostConcernedProblem",
						label: "您对植发手术最关注的问题",
						arr: [{
								name: "术后效果",
								value: "1",
								disabled: false,
							},
							{
								name: "手术技术",
								disabled: false,
								value: "2",
							},
							{
								name: "手术安全",
								disabled: false,
								value: "3",
							},
							{
								name: "手术价格",
								disabled: false,
								value: "4",
							},
							{
								name: "植发价格",
								disabled: false,
								value: "5",
							},
							{
								name: "其他",
								disabled: false,
								value: "9",
							},
						],
					},
					{
						type: "input",
						name: "hairTransplantSurgeryOtherValue",
						label: "您对植发手术最关注的问题",
					},
					{
						type: "radio",
						name: "hairDrynessMostConcernedProblem",
						label: "您对头皮发干养护最关注的问题",
						arr: [{
								name: "效果",
								value: "1",
								disabled: false,
							},
							{
								name: "产品",
								disabled: false,
								value: "2",
							},
							{
								name: "安全",
								disabled: false,
								value: "3",
							},
							{
								name: "价格",
								disabled: false,
								value: "4",
							},
							{
								name: "技术",
								disabled: false,
								value: "5",
							},
							{
								name: "其他",
								disabled: false,
								value: "9",
							},
						],
					},
					{
						type: "radio",
						name: "appointmentDate",
						label: "如果您需要手术，希望安排在哪天",
						arr: [{
								name: "当天",
								value: "1",
								disabled: false,
							},
							{
								name: "本周",
								disabled: false,
								value: "2",
							},
							{
								name: "本月",
								disabled: false,
								value: "3",
							},
							{
								name: "其他时间",
								disabled: false,
								value: "9",
							},
						],
					},
					{
						type: "date",
						name: "appointmentDateOtherValue",
						label: "预安排日期",
						show: false,
					},
					{
						type: "radio",
						name: "eyebrowProblem",
						label: "眉毛存在的问题",
						arr: [{
								name: "天生稀少",
								value: "1",
								disabled: false,
							},
							{
								name: "纹眉绣眉",
								disabled: false,
								value: "2",
							},
							{
								name: "切眉",
								disabled: false,
								value: "3",
							},
							{
								name: "疤痕",
								disabled: false,
								value: "4",
							},
							{
								name: "其他问题",
								disabled: false,
								value: "9",
							},
						],
					},
					{
						type: "input",
						name: "eyebrowProblemOtherValue",
						label: "眉毛存在的问题",
					},
					{
						type: "radio",
						name: "whiskersProblem",
						label: "胡须存在的问题",
						arr: [{
								name: "天生稀少",
								value: "1",
								disabled: false,
							},
							{
								name: "个性化设计",
								disabled: false,
								value: "2",
							},
							{
								name: "八字胡",
								disabled: false,
								value: "3",
							},
							{
								name: "其他问题",
								disabled: false,
								value: "9",
							},
						],
					},
					{
						type: "input",
						name: "whiskersProblemOtherValue",
						label: "胡须存在的问题",
					},
					{
						type: "textarea",
						name: "consultationRecord",
						label: "咨询记录",
					},
				],
				model: {
					openId: "",
					storeId: "",
					storeValue: "",
					customerId: "",
					weUserName: "",
					phoneNumber: "",
					email: "",
					wheChatId: "",
					sex: "",
					birthDate: "",
					birthDateType: "",
					age: "",
					profession: "",
					province: "",
					city: "",
					area: "",
					approach: "",
					lossHairTime: "",
					lossHairTimeValue: "",
					lossHairOfHistory: "",
					isDietRegular: "",
					sleepTime: "",
					isSmokingAndDrinking: "",
					hairCommonProblem: "",
					isUseMedication: "",
					consultProblem: "",
					hairTransplantUnderstandingDegree: "",
					hairCareUnderstandingDegree: "",
					consultOrTreatThePlace: "",
					operationMostConcernedProblem: "",
					hairTransplantSurgeryOtherValue: "",
					hairDrynessMostConcernedProblem: "",
					appointmentDate: "",
					appointmentDateOtherValue: "",
					eyebrowProblem: "",
					eyebrowProblemOtherValue: "",
					whiskersProblem: "",
					whiskersProblemOtherValue: "",
					consultationRecord: "",
				},
				rules: {},
				storeArr: [],
			};
		},
		components: {
			Registration,
		},
		methods: {
			submit() {
				let _this = this;
				delete this.$refs.Registration.model.id;
				let storeName = this.$refs.Registration.model.storeValue;
				_this.storeArr.forEach((item) => {
					if (item.name === storeName) {
						this.$refs.Registration.model.storeId = item.id;
					}
				});
				register(this.$refs.Registration.model).then((res) => {
					uni.$u.toast("信息登记表填写完成！");
					setTimeout(() => {
						_this.clue({
							relationId: "",
							action: "10",
							eventType: "1",
						});
						uni.navigateBack({
							delta: 1,
						});
					}, 2000);
				});
			},
		},
		onReady() {
			// this.$refs.Registration.$children[0].$refs.uForm.setRules(this.rules);
		},
		async created() {
			let openId = await uni.getStorageSync("openId");
			let _this = this;
			await allStore().then((res) => {
				let arr = res.map((item) => {
					return {
						name: item.name,
						id: item.value,
					};
				});
				_this.storeArr = arr;
				_this.listFrom.forEach((item) => {
					if (item.name === "storeValue") {
						item.arr.push(arr);
					}
				});
			});
			_this.model.openId = openId;
			_this.model.customerId = await uni.getStorageSync("customerId");

			// 处理 rules
			let rulesArr = {};
			let listFrom = JSON.parse(JSON.stringify(this.listFrom));
			listFrom.forEach((item) => {
				if (item.type === "radio") {
					rulesArr[item.name] = [{
						type: "string",
						required: true,
						message: "请选择" + item.label,
						trigger: ["blur", "change"],
					}, ];
				} else if (item.type === "select") {
					rulesArr[item.name] = [{
						type: "string",
						required: true,
						message: "请选择" + item.label,
						trigger: ["blur", "change"],
					}, ];
				} else if (item.type === "number") {
					rulesArr[item.name] = [{
						type: "number",
						required: true,
						message: "请选择" + item.label,
						trigger: ["blur", "change"],
					}, ];
				} else {
					rulesArr[item.name] = [{
						type: "string",
						required: true,
						message: "请输入" + item.label,
						trigger: ["blur", "change"],
					}, ];
				}
			});
			this.rules = rulesArr;
			console.log(rulesArr);
			this.$refs.Registration.$children[0].$refs.uForm.setRules(this.rules);
			// 历史数据回显
			await registerInfo({
				params: {
					openId,
				},
			}).then((res) => {
				console.log('res', res);
				if (res) {
					this.storeArr.forEach((item) => {
						if (item.id == res.storeId) {
							res.storeValue = item.name;
						}
					});
					_this.model = res;
				}
				_this.model.openId = openId;
			});
		},
	};
</script>
<style lang="scss">
	@import "./index.scss";
</style>